package com.base.cn.platform.os.controller.mobile.classes.topic;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.classes.ClassesService;
import com.base.cn.platform.os.service.course.classes.member.ClassesMemberService;
import com.base.cn.platform.os.service.course.classes.topic.ClassesTopicService;
import com.base.cn.platform.os.service.course.classes.topic.like.ClassesTopicLikeService;
import com.base.cn.platform.os.service.course.classes.topic.reply.ClassesTopicReplyService;
import com.base.cn.platform.os.service.course.classes.topic.type.ClassesTopicTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * H5班级话题Controller
 *
 * @author zsw
 * @create 2018/7/5
 */
@Controller
public class MobileClassTopicController extends MobileBaseController {

    @Autowired
    private ClassesService classesService;
    @Autowired
    private ClassesTopicService classesTopicService;
    @Autowired
    private ClassesTopicLikeService classesTopicLikeService;
    @Autowired
    private ClassesMemberService classesMemberService;
    @Autowired
    private ClassesTopicReplyService classesTopicReplyService;
    @Autowired
    private ClassesTopicTypeService classesTopicTypeService;

    /**
     * 加载话题列表
     *
     * @param classTopic 话题搜索条件
     */
    @RequestMapping("/mobile/class/topic/ajax/list/topic")
    public String listClassTopic(Model model,
                                 HttpServletRequest request,
                                 @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer page,
                                 @RequestBody Map<String, Object> classTopic) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        classTopic.put("userId",userId);
        classTopic.put("status",1);
        Map<String, Object> classesTopicListPage = classesTopicService.findClassesTopicListPage(classTopic, page, 5, 1, true, false, true);
        model.addAttribute("dataMap",classesTopicListPage);
        return resultPage("/class/topic/ajax-list-topic");
    }

    /**
     * 点赞话题
     *
     * @param topicId 话题id
     */
    @RequestMapping("/mobile/uc/class/topic/ajax/topic/like")
    @ResponseBody
    public Map<String, Object> likeTopic(HttpServletRequest request,
                                         @RequestParam("topicId") BigDecimal topicId) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return classesTopicLikeService.saveClassesTopicLike(topicId, userId);
    }

    /**
     * 发表话题
     *
     * @param request
     * @param topic
     * @return
     */
    @RequestMapping("/mobile/uc/class/topic/ajax/publish")
    @ResponseBody
    public Map<String, Object> publishTopic(HttpServletRequest request,
                                            @RequestBody Map<String,Object> topic) {
        BigDecimal createUserId = webMobileLoginUtil.getLoginUserId(request);
        topic.put("createUserId", createUserId);
        topic.put("source", 1);
        String classId = topic.get("classesId").toString();
        return classesTopicService.saveClassesTopic(topic, classId);
    }

    /**
     * 话题详情
     *
     * @param topicId 话题id
     */
    @RequestMapping("/mobile/class/topic/info")
    public String toTopicInfo(HttpServletRequest request,
                              Model model,
                              @RequestParam("id") BigDecimal topicId) {
        // 话题
        Map<String, Object> topic = classesTopicService.findClassesTopicById(topicId, true, true, true, true);
        model.addAttribute("topic", topic);
        // 是否登录
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        model.addAttribute("login", DataUtil.idIsNotNull(userId));
        // 是否加入班级
        Number classesId = (Number) topic.get("classesId");
        boolean joined = classesMemberService.joined(new BigDecimal(classesId.toString()), userId);
        model.addAttribute("joined", joined);
        if(!joined){//判断是否具有浏览权限
            Integer lookPermission = (Integer) topic.get("lookPermission");
            if(lookPermission==3 || lookPermission==2 && !DataUtil.idIsNotNull(userId)){
                return redirect(request,"/mobile/class/info?classId="+topic.get("classesId"));
            }
        }
        // 是否点过赞
        model.addAttribute("liked", classesTopicLikeService.checkUserLikeTopic(userId,topicId));
        // 班级详情
        Map<String, Object> classes = classesService.findClassesById(new BigDecimal(classesId.toString()), false, false, false, false, 0);
        model.addAttribute("clazz", classes);
        // 班级话题统计
        Map<String, Object> statistics = classesTopicService.classesTopicStatistics(topicId, null);
        model.addAttribute("statistics", statistics);
        return resultPage("/class/topic/topic-info");
    }

    /**
     * 查询评论列表
     *
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/mobile/class/topic/ajax/list/reply")
    public String listClassTopicReply(Model model,
                                      @RequestBody Map<String, Object> params,
                                      @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        Map<String, Object> reply = classesTopicReplyService.findClassesTopicReplyListPage(params, currentPage, 10, true, true, false,true);
        model.addAttribute("reply", reply);
        return resultPage("/class/topic/reply/ajax-topic-reply");
    }

    /**
     * 获取话题分类
     * @param classId
     * @return
     */
    @RequestMapping("/mobile/uc/class/topic/ajax/classesTopicTypeService")
    @ResponseBody
    public Map<String, Object> publishTopic(@RequestParam("classId") BigDecimal classId) {
    return ResultUtil.SUCCESS( classesTopicTypeService.getClassesTopicType(classId));
    }

}
